import { widgetList, DatasetList, getColumns, sourList } from '@/api/cboard/cboard'

const state = {
  widgetList: [],
  datasetList: [],
  datasourceList: [],
  providerList: []
}


const getters = {}


const actions = {

  //这两个获得的是 dashboard 菜单栏的菜单数据
  getWidgetList(context) {
    widgetList().then(response => {
      if(response.statusText === 'OK'){
        context.commit('setWidgetList', response.data)
      }
    }).catch((error) => {
      console.log(error)
    })
  },
  getDatasetList(context) {
    DatasetList().then((response) => {
      if(response.statusText === 'OK') {
        context.commit('setDatasetList', response.data);
      }
    }).catch((error) => {
      console.log(error)
    });
  },
  // 获取数据源
  getColumns(context, params) {
    return new Promise((resolve, reject) => {
      getColumns(params).then((response) => {
        resolve(response.data.columns);
      }).catch((error) => {
        console.log(error);
        reject(error);
      })
    })
  },
  // 获得数据源列表，数据集管理页面用到
  getDatasourceList(context) {
    return new Promise((resolve, reject) => {
      sourList().then(response => {
        if(response.statusText === 'OK') {
          context.commit('setDatasourceList', response.data);
          resolve(response.data);
        }
      })
      .catch(error => {
        console.log(error)
        reject(error);
      })
    })
  },
  getProviderList(context) {
    return new Promise((resolve, reject) => {
      req.get(api.getProviderList)
        .then(response => {
          context.commit('setProviderList', response.data);
          resolve(response.data);
        })
        .catch(error => {
          reject(error)
        })
    })
  }

}


const mutations = {
  setWidgetList(state, widgetList) {
    console.log('123')
    state.widgetList = widgetList;
  },
  setDatasetList(state, datasetList) {
    state.datasetList = datasetList;
  },
  setDatasourceList(state, datasourceList) {
    state.datasourceList = datasourceList;
  },
  setProviderList(state, payload) {
    state.providerList = payload;
  }
}


export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
